/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import model.*;
import dao.*;
import dao.DanhMucDAO;
import dao.DanhMucNhaSanXuatDAO;
import dao.LoaiDanhMucDAO;
import dao.SanphamDAO;
import dao.ThanhVienDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.jms.Session;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Danhmuc;
import model.DanhmucNhasanxuat;
import model.Hinhanh;
import model.Loaidanhmuc;
import model.Sanpham;
import model.Thanhvien;

/**
 *
 * @author HoangLiem
 */
@WebServlet(name = "firstPage", urlPatterns = {"/firstPage"})
public class firstPage extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //HttpSession session = request.getSession(true);
        try {
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(1200);
            session.setAttribute("dateNow", SanphamDAO.getNowDate());
            Cookie cookies[] = request.getCookies();
            String tenDN = "";
            String matKhau = "";
            if (cookies != null) {
                for (int i = 0; i < cookies.length; i++) {
                    if (cookies[i].getName().equals("username")) {
                        tenDN = cookies[i].getValue();
                    }
                    if (cookies[i].getName().equals("password")) {
                        matKhau = cookies[i].getValue();
                    }
                }
            }
            session.setAttribute("tendn", tenDN);
            session.setAttribute("matkhau", matKhau);
            ArrayList<Sanpham> dsClosingBid = SanphamDAO.getClosingBidProduct();
            ArrayList<Sanpham> dsNearPost = SanphamDAO.getNearestProduct();
            ArrayList<Sanpham> dsTopbid = new ArrayList<Sanpham>();
            ArrayList<String> TopbidString = SanphamDAO.getTopBid();
            ArrayList<String> dssoLuongbid = new ArrayList<String>();
            if (TopbidString.size() != 0) {
                for (int i = 0; i < TopbidString.size();) {
                    dsTopbid.add(SanphamDAO.getProduct(TopbidString.get(i)));
                    dssoLuongbid.add(TopbidString.get(i + 1));
                    i += 2;
                }
            }

            request.setAttribute("dsNearPost", dsNearPost);
            request.setAttribute("dsClosingBid", dsClosingBid);
            request.setAttribute("dsTopBid", dsTopbid);
            request.setAttribute("dssoLuongbid", dssoLuongbid);

            ArrayList<ArrayList<Hinhanh>> dshinh = new ArrayList<ArrayList<Hinhanh>>();

            ArrayList<ArrayList<Hinhanh>> dshinhNearPost = new ArrayList<ArrayList<Hinhanh>>();
            ArrayList<ArrayList<Hinhanh>> dshinhTopBid = new ArrayList<ArrayList<Hinhanh>>();

            dshinh = SanphamDAO.getAllImages(dsClosingBid);
            dshinhNearPost = SanphamDAO.getAllImages(dsNearPost);
            dshinhTopBid = SanphamDAO.getAllImages(dsTopbid);
            request.setAttribute("DSHinhAnh", dshinh);
            request.setAttribute("dshinhNearPost", dshinhNearPost);
            request.setAttribute("dshinhTopBid", dshinhTopBid);

            //Lay danh sach danh muc
            ArrayList<Loaidanhmuc> dsLoaiDanhMuc = LoaiDanhMucDAO.getAll();
            ArrayList<ArrayList<Danhmuc>> dayDanhMuc = new ArrayList<ArrayList<Danhmuc>>();
            if (dsLoaiDanhMuc.size() > 0) {
                for (int i = 0; i < dsLoaiDanhMuc.size(); i++) {
                    ArrayList<Danhmuc> dsDanhMuc = new ArrayList<Danhmuc>();
                    dsDanhMuc = DanhMucDAO.getAllByCategoryId(dsLoaiDanhMuc.get(i));
                    dayDanhMuc.add(dsDanhMuc);
                }
            }
            session.setAttribute("dsLoaiDanhMuc", dsLoaiDanhMuc);
            session.setAttribute("dayDanhMuc", dayDanhMuc);

            /*Danhmuc dm = DanhMucDAO.getCategoryById("ldm01dm005");
            Loaidaugia loaiDauGia = LoaiDauGiaDAO.getABidType(1);
            ArrayList<Sanpham> list = SanphamDAO.getProducts(dm,loaiDauGia);
            list = null;*/

            String url = "firstpagePage.jsp";
            RequestDispatcher rd = request.getRequestDispatcher(url);
            rd.forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
